package com.ruixi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruixi.config.BaseException;
import com.ruixi.entity.User;
import com.ruixi.mapper.UserMapper;
import com.ruixi.service.IUserService;
import com.ruixi.utils.PageBean;
import com.ruixi.utils.PageUtil;
import com.ruixi.utils.RedisUtils;
import com.ruixi.utils.Status;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author lrzg
 * @date 2021-10-18 11:27
 * @Description TODO
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Override
    public User getById(Long id) {
        return userMapper.getById(id);
    }

    @Override
    public User getUserByName(String name) {
        User user = (User) redisUtils.get(name);
        if(ObjectUtils.isEmpty(user)){
            user = userMapper.getUserByName(name);
            log.info("走缓存{}：",user.toString());
            redisUtils.set(user.getUsername(),user);
        }
        return user;
    }

    @Override
    public List<User> getUserListByName(String name) {
        return userMapper.getUserListByName(name);
    }

    @Override
    public PageBean<User> getUserListPage(PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<User> allUser = userMapper.findAllUser();
        return PageUtil.getPageVO(allUser);
    }

    @Override
    public int addUser(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }

    @Override
    public User updateUserByName(User user) {
        User userInfo = userMapper.getUserByName(user.getUsername());
        userInfo.setAddress(user.getAddress());
        int result = userMapper.updateById(user);
        return userMapper.getById(user.getId());
    }

    @Override
    public User updateUserById(User user) {
        User userInfo = userMapper.getById(user.getId());
        if(ObjectUtils.isEmpty(userInfo)){
            throw new BaseException(Status.SYSTEM_ERROR.code,Status.SYSTEM_ERROR.message);
        }
        userInfo.setAddress(user.getAddress());
        int result = userMapper.updateById(user);
        return userMapper.getById(user.getId());
    }

    @Override
    public int updateUser(User user) {
        User userInfo = userMapper.getById(user.getId());
        if(ObjectUtils.isEmpty(userInfo)){
            throw new BaseException(Status.SYSTEM_ERROR.code,Status.SYSTEM_ERROR.message);
        }
        userInfo.setUsername(user.getUsername());
        userInfo.setAddress(user.getAddress());
        return userMapper.updateUser(userInfo);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.saveUser(user);
    }

    @Override
    public int deleteUserById(Long id) {
        return userMapper.deleteUserById(id);
    }
}
